﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Newtonsoft.Json;

namespace Spider.Dual_coloredBall
{
    internal class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            SpiderInfo sInfo = new SpiderInfo();
            sInfo.GetMaxGameNum();

            int startYear = 2003;
            string LastGameNum = "";
            if (sInfo.ID > 0)
            {
                startYear = Convert.ToInt32(sInfo.LastYear);
                LastGameNum = sInfo.LastTime;
            }
            int endYear = DateTime.Now.Year;
            while (startYear <= endYear)
            {
                Console.WriteLine(startYear);

                //抓取地址
                string urlBasic = "http://baidu.lecai.com/lottery/draw/list/50?d=";
                string urlAboutYear = urlBasic + startYear + "-01-01";
                string html = new SpiderNet().GetHtmlContentByWebClient(urlAboutYear);
                MatchHtmlResult(startYear, html, endYear, LastGameNum);
                startYear++;
            }

            Console.WriteLine("OK");

            Console.ReadLine();

        }

        /// <summary>
        /// 正则匹配Html内容获取数据
        /// </summary>
        public static void MatchHtmlResult(int year, string htmlContent, int lastYear, string gameNum)
        {
            Regex regex = new Regex(@"<table.*?>[\s\S]*?<\/table>");
            MatchCollection mc = regex.Matches(htmlContent);
            GameResults gResults = new GameResults();
            gResults.GameYear = year;

            if (mc.Count > 0)
            {
                string tableHtml = mc[0].ToString();
                var regexBody = new Regex(@"<tbody.*?>[\s\S]*?<\/tbody>");
                string tBody = regexBody.Matches(tableHtml)[0].ToString();
                //打印逐行结果
                var regexTr = new Regex(@"<tr.*?>[\s\S]*?<\/tr>");
                MatchCollection mcTr = regexTr.Matches(tBody);
                foreach (var c in mcTr)
                {
                    string trHtml = c.ToString();
                    var regexTd = new Regex(@"<td.*?>[\s\S]*?<\/td>");
                    MatchCollection mcTd = regexTd.Matches(trHtml);

                    string[] aHref = GetAHrefValue(mcTd[1].ToString());

                    gResults.GameNum = aHref[0];
                    gResults.GameDate = Convert.ToDateTime(GetTdValue(mcTd[0].ToString()));
                    string[] strRedNumbers = GetRedBlueBall(mcTd[2].ToString());
                    gResults.RedOne = Convert.ToInt32(strRedNumbers[0]);
                    gResults.RedTwo = Convert.ToInt32(strRedNumbers[1]);
                    gResults.RedThree = Convert.ToInt32(strRedNumbers[2]);
                    gResults.RedFour = Convert.ToInt32(strRedNumbers[3]);
                    gResults.RedFive = Convert.ToInt32(strRedNumbers[4]);
                    gResults.RedSix = Convert.ToInt32(strRedNumbers[5]);
                    gResults.BlueSeven = Convert.ToInt32(strRedNumbers[6]);

                    gResults.SalesVolume = Convert.ToInt32(GetTdValue(mcTd[3].ToString()).Replace(",", ""));
                    if (lastYear == gResults.GameYear && !string.IsNullOrEmpty(gameNum))
                    {
                        if (Convert.ToInt32(gameNum) >= Convert.ToInt32(gResults.GameNum))
                        {
                            return;

                        }
                    }

                    gResults.Add();

                    //获取详细信息
                    string desUrl = "http://baidu.lecai.com" + aHref[1];
                    string descHtml = new SpiderNet().GetHtmlContentByWebClient(desUrl);
                    MatchHtmlResultsDetail(year, gResults.GameNum, descHtml);
                    //增加上次请求的信息
                    SpiderInfo sInfo = new SpiderInfo();
                    sInfo.LastYear = year;
                    sInfo.LastTime = gResults.GameNum;
                    sInfo.Add();
                }

            }
        }

        public static void MatchHtmlResultsDetail(int year, string gameNum, string htmlContent)
        {
            string descHtml = htmlContent;
            descHtml = descHtml.Substring(descHtml.IndexOf("var phaseData = ") + ("var phaseData = ").Length);
            descHtml = descHtml.Substring(0, descHtml.IndexOf("var result_config_arr =") - 1);
            descHtml = descHtml.Substring(descHtml.IndexOf("\"" + gameNum + "\": : {\"list\": ") + ("\"" + gameNum + "\": : {\"list\"").Length);
            descHtml = descHtml.Substring(0, descHtml.IndexOf("\"result\"") - 1);

            Dictionary<string, BallResult> lstBallAllDescr = JsonConvert.DeserializeObject<Dictionary<string, BallResult>>(descHtml);
            AddGameDetail(gameNum, "一等奖", lstBallAllDescr["prize1"].BallBet, lstBallAllDescr["prize1"].BallPrize);
            AddGameDetail(gameNum, "二等奖", lstBallAllDescr["prize2"].BallBet, lstBallAllDescr["prize2"].BallPrize);
            AddGameDetail(gameNum, "三等奖", lstBallAllDescr["prize3"].BallBet, lstBallAllDescr["prize3"].BallPrize);
            AddGameDetail(gameNum, "四等奖", lstBallAllDescr["prize4"].BallBet, lstBallAllDescr["prize4"].BallPrize);
            AddGameDetail(gameNum, "五等奖", lstBallAllDescr["prize5"].BallBet, lstBallAllDescr["prize5"].BallPrize);
            AddGameDetail(gameNum, "六等奖", lstBallAllDescr["prize6"].BallBet, lstBallAllDescr["prize6"].BallPrize);

        }

        public static void AddGameDetail(string gameNum, string prizeName, int sameNum, decimal moneyAward)
        {
            GameResultsDetail gDetail = new GameResultsDetail();
            gDetail.GameNum = gameNum;
            gDetail.PrizeName = prizeName;
            gDetail.Condition = "";
            gDetail.SameNum = sameNum;
            gDetail.MoneyAward = moneyAward;
            gDetail.Add();
        }

        //List<Dictionary<string, BallDescr>> lst = new List<Dictionary<string, BallDescr>>();

        public class BallDescr
        {
            [JsonProperty(PropertyName = "list")]
            public Dictionary<string, BallResult> BallList { get; set; }
        }

        public class BallResult
        {
            [JsonProperty(PropertyName = "key")]
            public string BallKey { get; set; }
            [JsonProperty(PropertyName = "name")]
            public string KeyName { get; set; }
            [JsonProperty(PropertyName = "bet")]
            public int BallBet { get; set; }
            [JsonProperty(PropertyName = "prize")]
            public int BallPrize { get; set; }
        }

        public static string GetTdValue(string tdHtml)
        {
            Regex regexTdValue = new Regex(@">[\s\S]*?<");
            MatchCollection mcTd = regexTdValue.Matches(tdHtml);
            return mcTd[0].ToString().TrimStart('>').TrimEnd('<');
        }

        public static string[] GetAHrefValue(string tdHtml)
        {
            Regex regexTdValue = new Regex(@"<a.*?>[\s\S]*?<\/a>");
            MatchCollection mcA = regexTdValue.Matches(tdHtml);
            string aHtml = mcA[0].ToString();
            string aValue = GetTdValue(aHtml);
            string tempA = aHtml.Substring(aHtml.IndexOf("\"", System.StringComparison.Ordinal) + 1);
            string hrlUrl = tempA.Substring(0, tempA.IndexOf("\"", System.StringComparison.Ordinal));
            return new string[] { aValue, hrlUrl };
        }

        public static string[] GetRedBlueBall(string tdHtml)
        {
            string tempData =
                tdHtml.Replace("<span class=\"result\">", "")
                    .Replace("</span>", "")
                    .Replace("<span class=\"ball_1\"", "")
                    .Replace("<span class=\"ball_2\"", "");
            tempData = GetTdValue(tempData).Replace("\n", "").Replace(" ", "").TrimStart('>');
            return tempData.Split('>');
        }

        public class SpiderNet
        {
            public string GetHtmlContentByWebClient(string url)
            {
                WebClient client = new WebClient();
                //获取或设置用于对Internet资源的请示进行身份验证的网络凭据
                client.Credentials = CredentialCache.DefaultCredentials;
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                //从指定网站下载数据
                byte[] data = client.DownloadData(url);

                //如果获取网站页面采用的是GB2312，则使用这句
                //string html = Encoding.Default.GetString(data);

                //如果获取网站页面采用的是UTF-8，则使用这句
                //string html = Encoding.UTF8.GetString(data);

                //使用默认编码
                string html = Encoding.UTF8.GetString(data);

                return html;
            }

            public string GetHtmlContentByWebRequest(string url)
            {
                try
                {

                    //构造web请示，发送请示，获取响应
                    WebRequest request = WebRequest.Create(url);
                    WebResponse response = request.GetResponse();

                    //获得流
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    string html = sr.ReadToEnd();
                    return html;
                }
                catch (Exception ex)
                {

                    throw ex;
                }
                return "";
            }

        }

    }
}
